package com.mxpio.erp.plan.mrp.policy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mxpio.erp.plan.mrp.Context;
import com.mxpio.erp.plan.mrp.CountModelMap;
import com.mxpio.erp.plan.mrp.bo.CountModel;
import com.mxpioframework.common.util.BeanReflectionUtils;

public abstract class LrpPurchaseOrderPolicy implements LrpPolicy {
	@Override
	public void run(Context context) {
		before(context);
		apply(context);
		after(context);
	}
	
	@Override
	public void before(Context context) {
		
		/*
		 * Map<Integer,String> fields = new HashMap<Integer,String>(); List<String>
		 * fieldlist = context.getItemIndexFields(); for(int i =
		 * 0;i<fieldlist.size();i++) { fields.put(i, fieldlist.get(i)); }
		 * List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		 * Map<String,List<String>> dic = getDic(); calculateCombination(dic, 0,
		 * fields,new String[dic.size()], result);
		 */
		 
	}
	
	@Override
	public void after(Context context) {
		
	}
	
	public void getAllIndex(Map<String,CountModel> storage) {
		if(storage == null) {
			storage = new HashMap<String,CountModel>();
		}
	}
	
	public void indexProcessAdd(Context context,Object entity, CountModelMap<List<CountModel>> result, CountModel countModel) throws Exception {
		String itemCode = BeanReflectionUtils.getPropertyValue(entity, "itemCode");
		StringBuffer sb = new StringBuffer();
		//遍历索引
		for(String itemIndexField : context.getItemIndexFields()) {
			sb = sb.append(BeanReflectionUtils.getPropertyValue(entity, itemIndexField)+",");
		}
		if(sb.length() != 0) {
			sb.setLength(sb.length() - 1);
		}
			
		countModel.setIndexes(sb.toString());
		
		Map<String, List<CountModel>> resultMap = result.get(itemCode);
		if(resultMap == null) {
			resultMap = new HashMap<String, List<CountModel>>();
		}
		
		List<CountModel> countModels = resultMap.get(sb.toString());
		if(countModels == null){
			countModels = new ArrayList<CountModel>();
		}
		
		countModels.add(countModel);
		resultMap.put(sb.toString(), countModels);
		result.put(itemCode, resultMap);
		
	}


	/**
     * 递归计算所有组合
     * @param inputList 所有数组的列表，数组用List<String>存储
     * @param beginIndex 代表每一个数组的在inputList中的索引
     * @param fields 数组别名
     * @param arr 用于保存每一次递归生成的组合
     * @param result 结果
     * */
    public void calculateCombination(Map<String,List<String>> inputList, int beginIndex, Map<Integer,String> fields, String[] arr, List<Map<String,String>> result) {
        if(beginIndex == inputList.size()){
        	Map<String,String> map = new HashMap<>();
        	for (int k = 0 ; k < arr.length; k++) {
            	map.put(fields.get(k), arr[k]);
            }
        	result.add(map);
            return;
        }
        for(String c: inputList.get(fields.get(beginIndex))){
            arr[beginIndex] = c;
            calculateCombination(inputList, beginIndex + 1, fields, arr, result);
        }
    }
}
